import {AbstractControl, FormGroup, ValidatorFn, Validators as ngValidators} from '@angular/forms';

export const GnValidators: { [key: string]: any } = {
  required: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.required;
  },
  min: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.min(args[0]);
  },
  max: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.max(args[0]);
  },
  maxLength: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.maxLength(args[0]);
  },
  minLength: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.minLength(args[0]);
  },
  requiredTrue: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.requiredTrue;
  },
  email: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.email;
  },
  pattern: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    return ngValidators.pattern(args[0]);
  },
  equalsTo: (formGroup: FormGroup, args: any[]): ValidatorFn => {
    const compareTo = formGroup.controls[args[0]];
    return (control: AbstractControl): { [key: string]: any } => {
      if (control.value !== compareTo.value) {
        return {equalsTo: {controlValue: control.value, compareToValue: compareTo.value}};
      }
      return null;
    };
  }
};

